package www.wen.com.common.lib.extension

import android.text.TextWatcher
import android.widget.SeekBar

/**
 * @author WEN
 * @Description: seekBar 的扩展类
 * @date 2021/7/1 19:26
 */


/**
 * Add an action which will be invoked before the text changed.
 *
 * @return the [TextWatcher] added to the TextView
 */
inline fun SeekBar.onProgressChanged(
        crossinline action: (
                seekBar: SeekBar,
                progress: Int,
                fromUser: Boolean
        ) -> Unit
) = addOnPageChangeListener(onProgressChanged = action)


/**
 * Add an action which will be invoked when the text is changing.
 *
 * @return the [TextWatcher] added to the TextView
 */
inline fun SeekBar.onStartTrackingTouch(
        crossinline action: (
                seekBar: SeekBar
        ) -> Unit
) = addOnPageChangeListener(onStartTrackingTouch = action)

/**
 * Add an action which will be invoked after the text changed.
 *
 * @return the [TextWatcher] added to the TextView
 */
inline fun SeekBar.onStopTrackingTouch(
        crossinline action: (seekBar: SeekBar) -> Unit
) = addOnPageChangeListener(onStopTrackingTouch = action)

/**
 * Add a text changed listener to this TextView using the provided actions
 *
 * @return the [TextWatcher] added to the TextView
 */
inline fun SeekBar.addOnPageChangeListener(
        crossinline onProgressChanged: (
                seekBar: SeekBar, progress: Int, fromUser: Boolean
        ) -> Unit = { _, _, _ -> },
        crossinline onStartTrackingTouch: (
                seekBar: SeekBar
        ) -> Unit = { _ -> },
        crossinline onStopTrackingTouch: (seekBar: SeekBar) -> Unit = {}
): SeekBar.OnSeekBarChangeListener {
    val seekBarChangeListener = object : SeekBar.OnSeekBarChangeListener {
        override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
            onProgressChanged.invoke(seekBar, progress, fromUser)
        }

        override fun onStartTrackingTouch(seekBar: SeekBar) {
            onStartTrackingTouch.invoke(seekBar)
        }

        override fun onStopTrackingTouch(seekBar: SeekBar) {
            onStopTrackingTouch.invoke(seekBar)
        }
    }
    setOnSeekBarChangeListener(seekBarChangeListener)
    return seekBarChangeListener
}
